package services.interactif;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;


public class TraitDeFormat {

	public enum TEVENT{Hexa, Blanc, RC, Autre};
	public enum FORMAT{F1, F2, F3, F4};
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		Scanner sc = new Scanner(System.in);
			
		String laChaine;
		
		System.out.println("La chaine � convertir :");
		laChaine = sc.next();
		
		while(laChaine != "") {
			decoder(laChaine + (char)13);
			System.out.println("La chaine � convertir :");
			laChaine = sc.next();
		}
		
	}

	public static void decoder(String chaine) {
		String memHexa = "";

		TEVENT event;
		int etat = 0;
		int car;
		
		for(int i = 0; i < chaine.length(); ++i) {
			
			car = chaine.charAt(i);
			event = carToEvent(car);

			if(car == 10) {
				i++;
				car=chaine.charAt(i);
				event = carToEvent(car);
			}
			
			switch (etat){
			
			case 0 :
				switch (event) {
					
				case Hexa :
					etat = 1;
					memHexa = memHexa + (char)car;
					break;
					
				case Blanc :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case RC :
					// Sortie F4 nbOctet 1
					etat = 0;
					sortie(FORMAT.F4, memHexa);
					memHexa = "";
					break;
				
				case Autre :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				}
			break;
			
			case 1 :
				switch (event) {
				
				case Hexa :
					etat = 2;
					memHexa = memHexa + (char)car;
					break;
					
				case Blanc :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case RC :
					// Sortie F4 nbOctet 1
					etat = 0;
					sortie(FORMAT.F4, memHexa);
					memHexa = "";
					break;
				
				case Autre :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				}
			break;
			
			case 2 :
				switch (event) {
				
				case Hexa :
					etat = 4;
					memHexa = memHexa + (char)car;
					break;
					
				case Blanc :
					etat = 5;
					memHexa = memHexa + (char)car;
					break;
				
				case RC :
					// Sortie F1 nbOctet 1
					etat = 0;
					sortie(FORMAT.F1, memHexa);
					memHexa = "";
					break;
				
				case Autre :
					etat = 3;
					break;
				}
			break;
			
			case 3 :
				switch (event) {
				
				case Hexa :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
					
				case Blanc :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case RC :
					// Sortie F4 nbOctet 1
					etat = 0;
					sortie(FORMAT.F4, memHexa);
					memHexa = "";
					break;
				
				case Autre :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				}
			break;
			
			case 4 :
				switch (event) {
				
				case Hexa :
					etat = 6;
					memHexa = memHexa + (char)car;
					break;
					
				case Blanc :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case RC :
					// Sortie F4 nbOctet 1
					etat = 0;
					sortie(FORMAT.F4, memHexa);
					memHexa = "";
					break;
				
				case Autre :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				}
			break;
			
			case 5 :
				switch (event) {
				
				case Hexa :
					etat = 7;
					memHexa = memHexa + (char)car;
					break;
					
				case Blanc :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case RC :
					// Sortie F4 nbOctet 1
					etat = 0;
					sortie(FORMAT.F4, memHexa);
					memHexa = "";
					break;
				
				case Autre :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				}
			break;
			
			case 6 :
				switch (event) {
				
				case Hexa :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case RC :
					// Sortie F2 nbOctet 2
					etat = 0;
					sortie(FORMAT.F2, memHexa);
					memHexa = "";
					break;
				
				case Blanc :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case Autre :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				}
			break;
			
			case 7 :
				switch (event) {
				
				case Hexa :
					etat = 8;
					memHexa = memHexa + (char)car;
					break;
					
				case Blanc :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case RC :
					// Sortie F4 nbOctet 1
					etat = 0;
					sortie(FORMAT.F4, memHexa);
					memHexa = "";
					break;
				
				case Autre :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				}
			break;
			
			case 8 :
				switch (event) {
				
				case Hexa :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case RC :
					//Sortie F3 nbOctet 2
					etat = 0;
					sortie(FORMAT.F3, memHexa);
					memHexa = "";
					break;
				
				case Blanc :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				
				case Autre :
					etat = 3;
					memHexa = memHexa + (char)car;
					break;
				}
			break;
			}
		}
	
	}
	
	public static void sortie(FORMAT f, String hexa) {
		switch(f) {
		case F1:
			ecrireSortie("F1 " + Integer.parseInt(hexa, 16));
			break;
		case F2:
			ecrireSortie("F2 " + Integer.parseInt(hexa.substring(0, 2), 16) + 
					" " + Integer.parseInt(hexa.substring(2, 4), 16));
			break;
		case F3:
			ecrireSortie("F3 " + Integer.parseInt(hexa.substring(0, 2), 16) + 
					" " + Integer.parseInt(hexa.substring(3, 5), 16));
			break;
		case F4:
			ecrireSortie("F4 " + hexa);
			break;
		}
	}
	
	public static void ecrireSortie(String chaine) {
		System.out.println("Sortie :");
		System.out.println(chaine);
	}
	
	// Transforme un char en type TEVENT
	public static TEVENT carToEvent(int c) {
		// Hexa entre 0-9 ou AF
		if(((c >= 65) && (c <= 70)) || ((c >= 48) && (c <= 57)))
			return TEVENT.Hexa;
		// Espace = 32
		if(c == 32)
			return TEVENT.Blanc;
		// Retour chariot = 13
		if((c == 13))
			return TEVENT.RC;
		// Le reste
		return TEVENT.Autre;
	}

}
